from jackson import debug

import re
from django.db import models
from django import forms
# basic file stuff

class ResourceNameField(forms.Field):
    def clean(self, value):
        """
        Check that the resource name is not a CIDRized IPv4 address.
        """
        context = [__name__, "clean()"]
        debug.enter(context, {'value' : value})
        if not value:
            raise forms.ValidationError('Enter a name for the resource.')
        clean_value = value.strip()
        debug.comment(context, "clean_value:" + str(clean_value))
        cidr = re.compile("^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])/([0-3]?\d)$")
        debug.comment(context, "match:" + str(cidr.match(clean_value)))
        if cidr.match(clean_value):
            debug.leave(context, {'status' : 'validation error'})
            raise forms.ValidationError("Resource name cannot be a CIDRized IPv4 address.")
        else:
            debug.leave(context, {'status' : 'clean and validated'})
            return clean_value

class UploadFileForm(forms.Form):
    title = ResourceNameField(label='Resource Name')
    fileobj = forms.FileField(label='File')

class ChooseFileNameForm(forms.Form):
    filename = ResourceNameField(label='Resource Name')